import userStore from "~/stores/user";
import type { Posts } from "./post";
import {
  type FindLimitDto,
  type FindList,
  login as apiLogin,
} from "~/api/auth";

export interface Users {
  id: string;
  account: string;
  nickname: string;
  avatar: string;
  email: string;
  openid: string;
  role: string;
  posts?: Posts[];
  createTime: Date;
  updateTime: Date;
}

export interface UpdateUsers {
  account?: string;
  nickname?: string;
  avatar?: string;
  email?: string;
  openid?: string;
  role?: string;
}

export interface CreateUsersEntity {
  account: string;
  nickname: string;
  email?: string;
  password: string;
}

export interface FormCreateUsersEntity extends CreateUsersEntity {
  confirmPassword?: string;
}

export const createUser = async (user: CreateUsersEntity) => {
  return await useServerRequest<Users>("/users", {
    method: "POST",
    body: user,
  });
};

export const getUserInfo = async () => {
  //开始请求
  const res = await useServerRequest<Users>("/users/getUserInfo", {
    method: "GET",
  });
  return res?.data;
};

export enum UserRole {
  root = "root",
  admin = "admin",
  author = "author",
  visitor = "visitor",
}

export const roleOptions: {
  label: string;
  value: string;
}[] = [
  { label: "超级管理员", value: UserRole.root },
  { label: "管理员", value: UserRole.admin },
  { label: "作者", value: UserRole.author },
  { label: "访客", value: UserRole.visitor },
];
export interface QueryUserDto extends FindLimitDto {
  account?: string;
  nickname?: string;
  email?: string;
  role?: string | null;
}

export const findAll = (query?: QueryUserDto) => {
  return useServerRequest<FindList<Users>>("/users", {
    method: "GET",
    params: { ...query },
  });
};
export const updateOne = (id: string, body: UpdateUsers) => {
  return useServerRequest(`/users/${id}`, {
    method: "PATCH",
    body: body,
  });
};
export const myInfoUpdate = (body: UpdateUsers) => {
  return useServerRequest(`/users/updateMyInfo`, {
    method: "PATCH",
    body: body,
  });
};

export const resetPassword = (id: string) => {
  return useServerRequest(`/users/resetPassword/${id}`, {
    method: "PATCH",
  });
};

export interface UpdateUserPassWordDto {
  oldPassWord: string;
  newPassWord: string;
}
export const updatePassword = (body: UpdateUserPassWordDto) => {
  return useServerRequest(`/users/updatePassword`, {
    method: "PATCH",
    body: { ...body },
  });
};
